<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\WxTask;
use App\Wen\Utils\FieldUtils;
use App\Wen\Utils\Settings;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class WxDayTaskController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new WxTask(), function (Grid $grid) {
            $grid->model()->where('type', '=', 0);
            $grid->quickSearch(['id','name'])->placeholder('搜索任务ID，名字...');
            $grid->model()->orderBy('id', 'desc');
            $grid->column('id')->sortable();
            $grid->column('name', '名字');
            $grid->column('type', '类型');
            $grid->column('condition_type_0', '条件类型')->using(FieldUtils::getDayTaskTypes());
            $grid->column('condition_num', '条件数量');
            $grid->column('reward_type', '奖励类型')->using(FieldUtils::getGiftTypes());
            $grid->column('reward_num', '奖励数量');
            $grid->column('target_type', '跳转类型')->using(FieldUtils::getUrlTypes());
            $grid->column('target_id', '跳转ID');
//            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new WxTask(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('type');
            $show->field('condition_type_0');
            $show->field('condition_type_1');
            $show->field('condition_num');
            $show->field('reward_type');
            $show->field('reward_num');
            $show->field('target_type');
            $show->field('target_id');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new WxTask(), function (Form $form) {
            $form->display('id');
            $form->text('name', '名字')->required();
            $form->radio('type', '类型')->options([0 => '每日任务'])->default(0)->required();
            $form->select('condition_type_0', '条件类型')->options(FieldUtils::getDayTaskTypes())->default(0)->required();
            $form->number('condition_num', '条件数量')->required();
            $form->select('reward_type', '奖励类型')->options(FieldUtils::getGiftTypes())->required();
            $form->number('reward_num', '奖励数量')->required();
            $form->radio('target_type', '跳转类型')->options(FieldUtils::getUrlTypes())->default(0);
            $form->text('target_id', '跳转ID')->help('<a href="https://doc.minisns.cn/doc/44/" target="_blank">路径大全</a>');
            $form->display('created_at');
            $form->display('updated_at');

            $form->deleting(function (Form $form){
                global $__MINI_GLOBAL_TENANT_ID__;
                if($__MINI_GLOBAL_TENANT_ID__ > 0){
                    return $form->response()->error('权限不足，不可以删除其他分站对象');
                }
            });

        });
    }
}
